﻿// JScript 文件
var m_xmin = 0, m_ymin = 0, m_xmax = 0, m_ymax = 0;
var m_oldxmin = 0, m_oldymin = 0, m_oldxmax = 0, m_oldymax = 0;
var m_Height = 0, m_Width = 0, m_pixelX = 0, m_pixelY = 0;
var m_widx = 0, m_widy = 0;
var m_ActieTool=0;
var m_MouseDown = new MouseDot(0,0)
var m_MouseUp = new MouseDot(0,0)

function MouseDot(x, y)
{
   this.x = x;
   this.y = y;
}

//定义点实体

function TePntDisp(id, enttype, rad, angle, x, y, bz)
{
    this.pntid   = id;
    this.pnttype = enttype;     
    this.pntrad  = rad;
    this.pntang  = angle;  
    this.xPos    = x; 
    this.yPos    = y;
    this.Bz      = bz;
}
//定义线实体

function TeLinDisp(id, enttype, fntpnt, talpnt, fntx, fnty, talx, taly)
{
    this.linid   = id;
    this.lintype = enttype;
    this.fntpnt  = fntpnt;
    this.talpnt  = talpnt;  
    this.fntx    = fntx; 
    this.fnty    = fnty;
    this.talx    = talx;  
    this.taly    = taly;  
}

function TeRect(left, top, right, bottom)
{
    this.left   = left;
    this.top    = top;
    this.right  = right;
    this.bottom = bottom;
}

//逻辑坐标转窗口坐标

function LpToWP(lx,ly)
{   
    m_widx = (lx - m_xmin)/m_pixelX
    m_widy = m_Height - (ly - m_ymin)/m_pixelY
}
//窗口坐标转逻辑坐标
function WpToLP(lx, ly)
{
	m_widx = m_pixelX * lx + m_xmin;
	var yTemp = (m_Height) - ly;
	m_widy = m_pixelY * yTemp + m_ymin;
}

function setWindowReg(x0, y0, x1, y1)
{
   m_oldxmin = m_xmin = parseFloat(x0)
   m_oldymin = m_ymin = parseFloat(y0)
   m_oldxmax = m_xmax = parseFloat(x1)
   m_oldymax = m_ymax = parseFloat(y1)
   m_Height = document.body.clientHeight;
   m_Width  = document.body.clientWidth;
   m_pixelX = (m_xmax - m_xmin) / m_Width;
   m_pixelY = (m_ymax - m_ymin) / m_Height;
}

function InitMapWnd(XmlDoc)
{
   //var XmlDoc = getXmlDoc();
   //if (XmlDoc != null)
   //{
       //XmlDoc.loadXML(entxml);
       var nodes = XmlDoc.selectNodes('NewDataSet');
	   if(nodes.length == 0)
	   {
		  return;
	   }
	   else
	   {
	      for (i = 0; i < nodes.length; ++i)
	      {
	          var vPnt = nodes[i].selectNodes("pnt");
	          for (l = 0; l < vPnt.length; ++l)
	          {
	              var vId   = vPnt[l].selectNodes("ID0");
				  var vType = vPnt[l].selectNodes("TYPE");
				  var vRad  = vPnt[l].selectNodes("RAD");
				  var vAng  = vPnt[l].selectNodes("ANG");
				  var vX    = vPnt[l].selectNodes("X");
				  var vY    = vPnt[l].selectNodes("Y");
				  var vBz   = vPnt[l].selectNodes("BZ");
				  
				  LpToWP(vX[0].text, vY[0].text)
				  var vDiv = document.createElement("div")
				  vDiv.style.left = 0
				  vDiv.style.top = 0
				  vDiv.entType = "Pnt"
				  vDiv.DevID   = "Pnt"+vId[0].text
				  var vScript = "<v:oval id='Pnt"+vId[0].text+"' xPos="+(m_widx-5)+" yPos="+(m_widy-5)+" fillcolor='red' style='position:absolute;left:"+(m_widx-5)+"px;top:"+(m_widy-5)+"px;width:10px;height:10px' />"
				  //vScript += "<span style='z-index:2;position:absolute;font-size:9px;left:"+(m_widx-40)+";top:"+(m_widy-20)+";'>"+vBz[0].text+"</span>"
				  vDiv.innerHTML = vScript;
				  divmap.appendChild(vDiv)
	          }
	          var vLin = nodes[i].selectNodes("lin");
	          for (l = 0; l < vLin.length; ++l)
	          {
	              var vId   = vLin[l].selectNodes("ID0");
				  var vType = vLin[l].selectNodes("TYPE");
				  var vFnt  = vLin[l].selectNodes("FNTPNT");
				  var vTal  = vLin[l].selectNodes("TALPNT");
				  var vX0   = vLin[l].selectNodes("X0");
				  var vY0   = vLin[l].selectNodes("Y0");
				  var vX1   = vLin[l].selectNodes("X1");
				  var vY1   = vLin[l].selectNodes("Y1");
				  
				  LpToWP(vX0[0].text, vY0[0].text)
				  var vDx1 = m_widx
				  var vDy1 = m_widy
				  LpToWP(vX1[0].text, vY1[0].text)
				  var vDx2 = m_widx
				  var vDy2 = m_widy

				  var vDiv = document.createElement("div")
				  vDiv.style.left = 0
				  vDiv.style.top = 0
				  vDiv.DevID = "Lin"+vId[0].text
				  vDiv.entType = "Lin"
				  var vScript = "<v:line id='Lin"+vId[0].text+"' Sl="+vDx1+" St="+vDy1+" Fx="+vDx2+" Fy="+vDy2+" style='LEFT:"+vDx1+"px;position:absolute;TOP:"+vDy1+"px' from='0,0' to='"+(vDx2-vDx1)+","+(vDy2-vDy1)+"'/>"
				  vDiv.innerHTML = vScript;
				  divmap.appendChild(vDiv)
	          }
	      }
	      
	   }
   //}
}



function getEntRectList(x0, y0, x1, y1)
{
    top.CtnDlg.loading("系统查询中，请稍候...");
    divmap.innerHTML = ""
    //获取XMLHTTPREQUEST对象
//    var xmlHttp = getXmlHttp()
//    var serverUrl = "/main.ashx?_method=getpnt&x0="+x0+"&y0="+y0+"&x1="+x1+"&y1="+y1;
//    if(xmlHttp)
//	{
//	   //请求
//	   xmlHttp.open("post",serverUrl, true) ;
//	   xmlHttp.setrequestheader("content-type","application/x-www-form-urlencoded") ;
//	   //发送

//	   xmlHttp.send(null) ;
//	   
//	   //
//	   xmlHttp.onreadystatechange = function()
//	  {
//			if(xmlHttp.readyState == 4)
//			{
//				if(xmlHttp.status == 200)
//				{
//				    //做事
//				    InitMapWnd(xmlHttp.responseText)
//				}
//			}
//		}
//	}
    var xmlDoc = CtnTrans.getXml("/main.ashx?", "POST", "_method=getpnt&x0="+x0+"&y0="+y0+"&x1="+x1+"&y1="+y1); 
    top.CtnDlg.unloading();
    if (!CtnError.validate(xmlDoc))
    {
        CtnError.show();
        return;
    }
    InitMapWnd(xmlDoc);
}

function UpdateEntRectList(x0, y0, x1, y1)
{
//    var xmlHttp = getXmlHttp()
//    var serverUrl = "/main.ashx?_method=getpnt&x0="+x0+"&y0="+y0+"&x1="+x1+"&y1="+y1;
//    if(xmlHttp)
//	{
//	   xmlHttp.open("post",serverUrl, true) ;
//	   xmlHttp.setrequestheader("content-type","application/x-www-form-urlencoded") ;
//	   xmlHttp.send(null) ;
//	   xmlHttp.onreadystatechange = function()
//	  {
//			if(xmlHttp.readyState == 4)
//			{
//				if(xmlHttp.status == 200)
//				{
//				
//				}
//			}
//		}
//	}
    top.CtnDlg.loading("系统查询中，请稍候...");
    var xmlDoc = CtnTrans.getXml("/main.ashx?", "POST", "_method=getpnt&x0="+x0+"&y0="+y0+"&x1="+x1+"&y1="+y1); 
    top.CtnDlg.unloading();
    if (!CtnError.validate(xmlDoc))
    {
        CtnError.show();
        return;
    }
}

function Mapmousemove()
{
    
}

function Mapmouseout()
{
   
}

function Mapmouseover()
{
   
}

function Mapmouseup()
{
   m_MouseUp.x  = event.clientX;
   m_MouseUp.y  = event.clientY;
    
   switch (m_ActieTool)
   {
       case 1001:
            
            break;
       case 1002:
            break;
       case 1003:
       {
            WpToLP(m_MouseUp.x, m_MouseUp.y)
            var vMouseUpX = m_widx
            var vMouseUpY = m_widy
            WpToLP(m_MouseDown.x, m_MouseDown.y)
            var vMouseDownX = m_widx
            var vMouseDownY = m_widy
            var vXArea = vMouseUpX-vMouseDownX
            var vYArea = vMouseUpY-vMouseDownY
            
            var vXClient = m_MouseUp.x - m_MouseDown.x
            var vYClient = m_MouseUp.y - m_MouseDown.y
            
            //当前地图显示范围
            m_xmin = m_xmin - vXArea;
            m_ymin = m_ymin - vYArea;
            m_xmax = m_xmax - vXArea;
            m_ymax = m_ymax - vYArea;
            
            //删除移出的部分

            //移动保留的部分

            delEntRect(vXClient, vYClient)
            //添加更新的部分

            
       }
            break;
       case 1004:
            break;
       default:
            break;
   }
   
}

function IsPntInRect(x, y, rect)
{
    var vIs = false
    if ((x>=rect.left)&&(x<=rect.right)&&(y>=rect.top)&&(y<=rect.bottom))
    {
        vIs = true
    }
    return vIs;
}

function IsLinInRect(x0, y0, x1, y1, rect)
{
    var vIs = false
    
    if (x0>= rect.left && y0 >= rect.top && x1 <= rect.right && y1 <= rect.bottom)
    {
        vIs = true
    }
    return vIs;
}

//方向   方向 X  Y
//左上-〉右下 正 负 正 正

//右下-〉左上 负 正 负 负

//右上-〉左下 负 负 负 正

//左下-〉右上 正 正 正 负

//正上-〉正下 0  负

//正下-〉正上 0  正

//正左-〉正右 正 0
//正右-〉正左 负 0
function delEntRect(x, y)
{
    var vRect;
    
    if (x > 0 && y >= 0)
    {
        vRect = new TeRect(0, 0, (m_Width-x), (m_Height - y))
    }
    else if (x < 0 && y <= 0)
    {
        vRect = new TeRect(-x, -y, m_Width, m_Height)
    }
    else if (x <= 0 && y > 0)
    {
        vRect = new TeRect(0, y, (m_Width+x), m_Height)
    }
    else if (x >= 0 && y < 0)
    {
        vRect = new TeRect(x, 0, m_Width, (m_Height+y))
    }
    
    var vCount = divmap.childNodes.length
    for (var i = vCount -1; i >= 0; i--)
    {
       if (divmap.childNodes[i].entType == "Pnt")
       {
           var vPnt = document.getElementById(divmap.childNodes[i].DevID)
           if (vPnt)
           {
               if (!IsPntInRect(vPnt.xPos, vPnt.yPos, vRect))
               {
                    divmap.childNodes[i].innerHTML = ""
                    divmap.removeChild(divmap.childNodes[i])
               }
               else
               {
                   vPnt.style.left = vPnt.xPos =  parseInt(vPnt.xPos) + x
                   vPnt.style.top  = vPnt.yPos =  parseInt(vPnt.yPos) + y
               }
           }
        }
        else if (divmap.childNodes[i].entType == "Lin")
        {
            var vLin = document.getElementById(divmap.childNodes[i].DevID)
            if (vLin)
            {
               if (!IsLinInRect(vLin.Sl, vLin.St, vLin.Fx, vLin.Fy, vRect))
               {
                   divmap.childNodes[i].innerHTML = "";
                   divmap.removeChild(divmap.childNodes[i]);
               }
               else
               {
                   vLin.style.left = vLin.Sl =  parseInt(vLin.Sl) + x
                   vLin.style.top  = vLin.St =  parseInt(vLin.St) + y
                   vLin.Fx =  parseInt(vLin.Fx) + x
                   vLin.Fy =  parseInt(vLin.Fy) + y
               }
            }
        }
    }
}

//方向   方向 X  Y
//左上-〉右下 正 负

//右下-〉左上 负 正

//右上-〉左下 负 负

//左下-〉右上 正 正

//正上-〉正下 0  负

//正下-〉正上 0  正

//正左-〉正右 正 0
//正右-〉正左 负 0

function Mapmousedown()
{
   if ($("rightdiv").style.visibility == "visible")
   {
       $("rightdiv").style.visibility = "hidden"
   }
   m_MouseDown.x  = event.clientX;
   m_MouseDown.y  = event.clientY;
   
}

function Onrightclick()
{
    $("rightdiv").innerHTML = ""
    $("rightdiv").style.visibility = "visible"
    var vRightDiv = document.createElement("div")
    vRightDiv.style.zIndex = "1"
    var vMenu = "<table border=1 bordercolor='black'>";
    vMenu += "<tr><td bgcolor='gray' height=20px><font size=2><a href='javascript:zoomoutwnd()'>放大窗口</a></font></td></tr>";
    vMenu += "<tr><td bgcolor='gray' height=20px><font size=2><a href='javascript:zoominwnd()'>缩小窗口</a></font></td></tr>";
    vMenu += "<tr><td bgcolor='gray' height=20px><font size=2><a href='javascript:movewnd()'>移动窗口</a></font></td></tr>";
    vMenu += "<tr><td bgcolor='gray' height=20px><font size=2><a href='javascript:updatewnd()'>刷新窗口</a></font></td></tr>";
    vMenu += "</table>";
	vRightDiv.innerHTML = vMenu;
	$("rightdiv").appendChild(vRightDiv)
	$("rightdiv").style.left = event.clientX
	$("rightdiv").style.top  = event.clientY
}

function zoomoutwnd()
{
   divoper.style.cursor = "crosshair"
   m_ActieTool = 1001
}

function zoominwnd()
{
   divoper.style.cursor = "crosshair"
   m_ActieTool = 1002
}

function movewnd()
{
   divoper.style.cursor = "move"
   m_ActieTool = 1003
}

function updatewnd()
{
   divoper.style.cursor = "crosshair"
   m_ActieTool = 1004
}